package com.mine.photohandler.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.mine.photohandler.R;
import com.mine.photohandler.util.Bimp;
import com.mine.photohandler.util.PublicWay;


/**
 * Created by admin on 2016/11/17.
 */

public class PicFrameAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;

    public PicFrameAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void update() {
        this.notifyDataSetChanged();
    }

    public int getCount() {
        if(Bimp.tempSelectBitmap.size() == PublicWay.num){
            return PublicWay.num;
        }
        return (Bimp.tempSelectBitmap.size() + 1);
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_published_grida,
                    parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView
                    .findViewById(R.id.item_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == Bimp.tempSelectBitmap.size()) {
            holder.image.setImageBitmap(BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.icon_addpic_unfocused));
            holder.image.setVisibility(View.VISIBLE);
            if (position == PublicWay.num) {
                holder.image.setVisibility(View.GONE);
            }
        } else {
            if (Bimp.tempSelectBitmap.get(position).getBitmap() != null){
//                holder.image.setImageBitmap(BitmapFactory.decodeFile(Bimp.tempSelectBitmap.get(position).imagePath));
                holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
                holder.image.setVisibility(View.VISIBLE);
            }else {
//                    holder.sdv.setImageURI(Uri.parse(Bimp.tempSelectBitmap.get(position).getImageUri()));
            }
        }

        return convertView;
    }

    public class ViewHolder {
        public ImageView image;
    }
}
